package com.azt.data.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.azt.data.permission.MenuPermission;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.azt.api.model.Pagination;
import com.azt.api.service.PaginationService;
import com.azt.data.utils.CommonUtil;
import com.azt.data.utils.ExcelExportEntityUtil;
import com.azt.data.utils.Globals;
import com.azt.data.utils.RequestUtils;
import com.azt.provider.mapper.IntegralCoinMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaoleilu.hutool.util.StrUtil;

import net.sf.json.JSONArray;

/** 
 * @ClassName: IntegralCoinAction 
 * @Description: 积分和安装币
 * @author: zhaheng
 * @date: 2017年5月2日 下午2:59:00  
 */
@Controller
@RequestMapping(value = "/integralcoin")
public class IntegralCoinAction {
	@Autowired
	private IntegralCoinMapper integralCoinMapper;

	@Autowired
	private PaginationService paginationService;
	
	/** 
	 * @Title: IntegralCoinView 
	 * @Description: 积分概览
	 * @param model
	 * @return
	 * @return: String
	 * @author: 查恒 2017年5月3日 上午10:59:32 
	 */
	@MenuPermission
	@RequestMapping(value = "/integralCoinView")
	public String IntegralCoinView(ModelMap model) {
		// 积分消耗总数
		int integralUsedTotal = integralCoinMapper.getIntegralUsedTotal();
		// 兑换礼品总数
		int exchangePrizeCount = integralCoinMapper.getExchangePrizeCount();
		// 安装币消耗总数
		int usedCoinCount = integralCoinMapper.getUsedCoinCount();
		// 安装币获得总数
		int gainCoinCount = integralCoinMapper.getGainCoinCount();
		
		model.addAttribute("integralUsedTotal", integralUsedTotal);
		model.addAttribute("exchangePrizeCount", exchangePrizeCount);
		model.addAttribute("usedCoinCount", usedCoinCount);
		model.addAttribute("gainCoinCount", gainCoinCount);
		return "integralCoin/integralCoinView";
	}
	
	//每月用户获得安装币数量
	@ResponseBody
	@RequestMapping(value = "/gainCoinDateRate")
	public JSONObject gainCoinDateRate(String startDate, String endDate) {
		JSONObject json = new JSONObject();
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		if (StrUtil.isBlank(startDate)) {
			endDate = sdf.format(c.getTime());
			c.add(Calendar.MONTH, -11);
			startDate = sdf.format(c.getTime());
		}
		List<Map<String, Object>> gainCoinDateList = integralCoinMapper.getGainCoinDateRate(startDate, endDate);
		String jsonString = JSON.toJSONString(gainCoinDateList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		json.put("startDate", startDate);
		json.put("endDate", endDate);
		return json;
	}
	//每月用户消耗安装币数量
	@ResponseBody
	@RequestMapping(value = "/usedCoinDateRate")
	public JSONObject usedCoinDateRate(String startDate, String endDate) {
		JSONObject json = new JSONObject();
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		if (StrUtil.isBlank(startDate)) {
			endDate = sdf.format(c.getTime());
			c.add(Calendar.MONTH, -11);
			startDate = sdf.format(c.getTime());
		}
		List<Map<String, Object>> usedCoinDateList = integralCoinMapper.getUsedCoinDateRate(startDate, endDate);
		String jsonString = JSON.toJSONString(usedCoinDateList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		json.put("startDate", startDate);
		json.put("endDate", endDate);
		return json;
	}
	
	//每月用户获得积分数量
	@ResponseBody
	@RequestMapping(value = "/gainIntegralRate")
	public JSONObject gainIntegralRate(String startDate, String endDate) {
		JSONObject json = new JSONObject();
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		if (StrUtil.isBlank(startDate)) {
			endDate = sdf.format(c.getTime());
			c.add(Calendar.MONTH, -11);
			startDate = sdf.format(c.getTime());
		}
		List<Map<String, Object>> gainIntegralList = integralCoinMapper.getGainIntegralRate(startDate, endDate);
		String jsonString = JSON.toJSONString(gainIntegralList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		json.put("startDate", startDate);
		json.put("endDate", endDate);
		return json;
	}
	//积分各获得方式的数量占比
	@ResponseBody
	@RequestMapping(value = "/gainIntrgralPie")
	public JSONObject gainIntrgralPie() {
		JSONObject json = new JSONObject();
		List<Map<String, Object>> gainIntrgralPieList = integralCoinMapper.getGainIntrgralPie();
		String jsonString = JSON.toJSONString(gainIntrgralPieList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		return json;
	}
	
	//每月用户消耗积分数量
	@ResponseBody
	@RequestMapping(value = "/usedIntegralRate")
	public JSONObject usedIntegralRate(String startDate, String endDate) {
		JSONObject json = new JSONObject();
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		if (StrUtil.isBlank(startDate)) {
			endDate = sdf.format(c.getTime());
			c.add(Calendar.MONTH, -11);
			startDate = sdf.format(c.getTime());
		}
		List<Map<String, Object>> usedIntegralList = integralCoinMapper.getUsedIntegralRate(startDate, endDate);
		String jsonString = JSON.toJSONString(usedIntegralList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		json.put("startDate", startDate);
		json.put("endDate", endDate);
		return json;
	}
	//积分各消耗方式的数量占比
	@ResponseBody
	@RequestMapping(value = "/usedIntrgralPie")
	public JSONObject usedIntrgralPie() {
		JSONObject json = new JSONObject();
		List<Map<String, Object>> usedIntrgralPieList = integralCoinMapper.getUsedIntrgralPie();
		String jsonString = JSON.toJSONString(usedIntrgralPieList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		return json;
	}
	
	/**排名表**/
	//兑换的礼品数量排名前5的商品
	@ResponseBody
	@RequestMapping(value = "/viewGiftNumTable")
	public JSONObject viewGiftNumTable(){
		JSONObject json = new JSONObject();
		List<Map<String, Object>> viewGiftNumList=integralCoinMapper.getViewGiftNumTop();
		String jsonString = JSON.toJSONString(viewGiftNumList, CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		return json;
	}
	
	
	/********积分安装币列表********/
	@MenuPermission
	@RequestMapping(value = "/integralCoinDetail")
	public String integralCoinDetail(Integer type, ModelMap model) {
		if (type == null) {
			type = 0;
		}
		model.addAttribute("type", type);
		return "integralCoin/integralCoin_list";
	}
	
	@ResponseBody
	@RequestMapping(value = "/integralCoinData")
	public JSONObject integralCoinData(HttpServletRequest request, Integer pageNo,  Integer type,
			ModelMap model) {
		// 搜索条件
		Map searchMap = RequestUtils.getSearchMap(request);
		JSONObject json = new JSONObject();
		List<Map<String, Object>> selectList = null;
		Page<Object> startPage = null;
		// 列表序号
		if (type == null) {
			type = 0;
		}
		if (type.equals(1)) {
			//安装币流水（获得和消耗）
			startPage = PageHelper.startPage(pageNo == null ? 1 : pageNo, Globals.PAGE_SIZE);
			selectList = integralCoinMapper.selectCoinStream(searchMap);
		}else {
			//积分流水（获得和消耗）
			startPage = PageHelper.startPage(pageNo == null ? 1 : pageNo, Globals.PAGE_SIZE);
			selectList = integralCoinMapper.selectIntegralStream(searchMap);
		}

		Pagination paginationList = paginationService.getPaginationList(startPage, selectList);
		RequestUtils.MapToModelMap(searchMap, request);
		String jsonString = JSON.toJSONString(paginationList.getList(), CommonUtil.jsonfilter,
				SerializerFeature.WriteDateUseDateFormat);
		JSONArray fromObject = JSONArray.fromObject(jsonString);
		json.put("data", fromObject);
		json.put("draw", paginationList.getPageNo());
		json.put("total", paginationList.getTotalCount());
		json.put("recordsTotal", paginationList.getTotalCount());
		json.put("recordsFiltered", paginationList.getTotalCount());
		return json;
	}
	
	//导出列表Excel
	@ResponseBody
	@RequestMapping(value = "/exportIntegralCoinData")
	public void exportIntegralCoinData(Integer type, HttpServletRequest request, HttpServletResponse response) {
		Workbook exportExcel = null;
		Map searchMap = RequestUtils.getSearchMap(request);
		List<Map<String, Object>> selectList = null;
		String title = ".xls";
		if (type.equals(0)) {
			// 积分流水（获得和消耗）
			selectList = integralCoinMapper.selectIntegralStream(searchMap);
			exportExcel = ExcelExportUtil.exportExcel(
					new ExportParams(ExcelExportEntityUtil.INTEGRALCOIN_DETAIL_TITLE, "积分列表", "积分列表"),
					ExcelExportEntityUtil.integralStreamList(), selectList);
			title = ExcelExportEntityUtil.INTEGRALCOIN_DETAIL_TITLE + "积分列表" + title;
		} else if (type.equals(1)) {
			// 安装币流水（获得和消耗）
			selectList = integralCoinMapper.selectCoinStream(searchMap);
			exportExcel = ExcelExportUtil.exportExcel(
					new ExportParams(ExcelExportEntityUtil.INTEGRALCOIN_DETAIL_TITLE, "安装币列表", "安装币列表"),
					ExcelExportEntityUtil.coinStreamList(), selectList);
			title = ExcelExportEntityUtil.INTEGRALCOIN_DETAIL_TITLE + "安装币列表" + title;
		}

		try {
			response.setContentType("application/vnd.ms-excel;");
			response.setHeader("Content-Disposition", "attachment;filename=" + CommonUtil.toUtf8String(request.getHeader("User-Agent") , title));
			ServletOutputStream outputStream = response.getOutputStream();
			exportExcel.write(outputStream);
			outputStream.flush();
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	//导出积分安装币概览Excel
	@RequestMapping("/exporIntegralCoinDetailExcel")
	public void exporIntegralCoinDetailExcel(Integer type, String startDate, String endDate, HttpServletRequest request,
			HttpServletResponse response) {
		Workbook exportExcel = null;
		List<Map<String, Object>> selectList = null;
		String title = ".xls";
		if (type.equals(0)) {
			// 月用户获得安装币数量
			selectList = integralCoinMapper.getGainCoinDateRate(startDate, endDate);
			exportExcel = ExcelExportUtil.exportExcel(
					new ExportParams(ExcelExportEntityUtil.INTEGRALCOIN_VIEW_TITLE, "月用户获得安装币", "月用户获得安装币"),
					ExcelExportEntityUtil.gainCoinDateList(), selectList);
			title = ExcelExportEntityUtil.INTEGRALCOIN_VIEW_TITLE + "月用户获得安装币" + title;
		} else if (type.equals(1)) {
			// 月用户消耗安装币数量
			selectList = integralCoinMapper.getUsedCoinDateRate(startDate, endDate);
			exportExcel = ExcelExportUtil.exportExcel(
					new ExportParams(ExcelExportEntityUtil.INTEGRALCOIN_VIEW_TITLE, "月用户消耗安装币", "月用户消耗安装币"),
					ExcelExportEntityUtil.usedCoinDateList(), selectList);
			title = ExcelExportEntityUtil.INTEGRALCOIN_VIEW_TITLE + "月用户消耗安装币" + title;
		} else if (type.equals(2)) {
			// 月用户获得积分数量
			selectList = integralCoinMapper.getGainIntegralRate(startDate, endDate);
			exportExcel = ExcelExportUtil.exportExcel(
					new ExportParams(ExcelExportEntityUtil.INTEGRALCOIN_VIEW_TITLE, "月用户获得积分", "月用户获得积分"),
					ExcelExportEntityUtil.gainIntegralList(), selectList);
			title = ExcelExportEntityUtil.INTEGRALCOIN_VIEW_TITLE + "月用户获得积分" + title;
		} else if (type.equals(3)) {
			// 月用户消耗积分数量
			selectList = integralCoinMapper.getUsedIntegralRate(startDate, endDate);
			exportExcel = ExcelExportUtil.exportExcel(
					new ExportParams(ExcelExportEntityUtil.INTEGRALCOIN_VIEW_TITLE, "月用户消耗积分", "月用户消耗积分"),
					ExcelExportEntityUtil.usedIntegralList(), selectList);
			title = ExcelExportEntityUtil.INTEGRALCOIN_VIEW_TITLE + "月用户消耗积分" + title;
		}

		try {
			response.setContentType("application/vnd.ms-excel;");
			response.setHeader("Content-Disposition", "attachment;filename=" + CommonUtil.toUtf8String(request.getHeader("User-Agent") , title));
			ServletOutputStream outputStream = response.getOutputStream();
			exportExcel.write(outputStream);
			outputStream.flush();
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
}
